#  插入排序
from utils.array_utils import random_list,check_is_sorted


def sort(l):
    s = 0
    for i in range(len(l)):
        # 循环条件: l[i]>l[i-1]
        # print(f'循环{i}:')
        j = i
        n = l[i]
        while n < l[j - 1] and j > 0:
            l[j] = l[j - 1]
            j -= 1
            s += 1
        l[j] = n
    return s


def sort02(l):
    s = 0
    for i in range(1, len(l)):
        j = i
        n = l[i]
        # print('----', i)
        while n < l[j - 1] and j > 0:
            l[j] = l[j - 1]
            j -= 1
            s += 1
            # print(j, l)
        l[j] = n
    return s


if __name__ == '__main__':
    ll = [2, 8, 3, 7, 6, 5, 9, 1, 4, 0, -1, 2, -9, 5, 9, 1]

    # print('--', ll[::long//2])
    # print('--',ll[::long//4])
    # print('--',ll[::long//8])
    # print('--', ll[::long//16])

    # print(sort02(ll))
    # print(ll)

    # 对数器
    a = True
    # 1. 循环n次
    for i in range(100):
        # 2. 生成随机列表
        l = random_list(-10, 50, 50)
        l2 = l.copy()
        # 3. 用自己的函数排序
        sort02(l)
        # 4. 用自带的sort()排序
        l2.sort()
        # 5. 把两次排序内容都转成字符串并比较
        if l != l2:
            print('有问题', l, l2)
            a = False
    if a:
        print('没问题')

    print(check_is_sorted(sort))